<template>
  <div>
    <el-dialog
      title="授权共建"
      :visible.sync="dialogVisible"
      width="600px"
      :close-on-click-modal="false"
      @closed="resetForm('form')"
    >
      <div>
        <el-form ref="form" :rules="rules" :model="form" label-width="80px">
          <div style="width: 90%">
            <el-form-item label="姓名" prop="userName">
              <el-input v-model.trim="form.userName"></el-input>
            </el-form-item>
            <div class="account-box">
              <el-form-item label="账号" prop="account">
                <el-input
                  v-model.trim="form.account"
                  autocomplete="off"
                  @blur="handleBlur()"
                ></el-input>
              </el-form-item>
              <span
                v-show="accountAali == 0"
                class="el-icon-question tpic color-info"
              ></span>
              <span
                v-show="accountAali == 1"
                class="el-icon-success tpic color-success"
              ></span>
              <span
                v-show="accountAali == 2"
                class="el-icon-error tpic color-danger"
              ></span>
            </div>
            <el-form-item label="密码" prop="password">
              <el-input
                type="password"
                autocomplete="new-password"
                v-model.trim="form.password"
              ></el-input>
            </el-form-item>
          </div>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button  type="custom" v-preventReClick  @click="submitForm('form')">提 交</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { tbSysUserBatchUpdatePassword, tbUserVali } from "@/api/admin/user/user-manage.js";
import Bus from "@/utils/bus.js";
export default {
  data() {
    return {
      dialogVisible: false,
      accountAali: 0,
      form: {
        id: "",
        userName: "",
        account: "",
        password: "",
        // del: 1, //1.可用 2.不可用
        del: "2", //不可用，需审核
        definitionCode: "11",
      },
      rules: {
        userName: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        account: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  mounted() {
    Bus.$on("showDialogCourseBuildAudit", (v) => {
      this.form.id = v.sysUser.id;
      this.form.userName = v.sysUser.userName;
      this.dialogVisible = true;
    });
  },
  beforeDestroy() {
    Bus.$off("showDialogCourseBuildAudit");
  },
  methods: {
    submitForm(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          this.edit();
        }
      });
    },
    //修改用户
    edit() {
      var list = [];
      list.push(this.form)
      tbSysUserBatchUpdatePassword(list).then((res) => {
        if (res.data.code == 0) {
          Bus.$emit("refreshCourseTeam");
          this.dialogVisible = false;
          this.$message({
            message: "操作成功",
            type: "success",
          });
        }
      });
    },
    handleBlur() {
      if (!this.form.account) {
        this.accountAali = 0;
        return;
      }
      tbUserVali({ account: this.form.account }).then((res) => {
        if (res.data.msg == "0") {
          this.accountAali = 1;
        } else {
          this.accountAali = 2;
          this.$message({
            message: "账号已存在",
            type: "error",
          });
        }
      });
    },
    //重置表单
    resetForm(form) {
      this.accountAali = 0;
      this.$refs[form].resetFields();
      for (let key in this.form) {
        this.form[key] = "";
      }
    },
  },
};
</script>
<style scoped>
.account-box {
  position: relative;
}
.tpic {
  position: absolute;
  right: -30px;
  top: 10px;
  font-size: 20px;
}
</style>